package com.cunshe.web;

import com.auth0.jwt.JWT;
import com.cunshe.common.WebConsts;
import com.cunshe.domain.User;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class BaseController {

    public Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    public Gson gson;
    @Autowired
    public HttpSession session;
    @Autowired
    public HttpServletRequest request;
    @Autowired
    public HttpServletResponse response;

    /**
     * 获取当前登录用户
     */
    public User getCurrentUser() {
        User user = null;
        try {
            String token = request.getHeader("token");
            String json = JWT.decode(token).getClaim("user").asString();
            user = new Gson().fromJson(json, User.class);
        } catch (Exception e) {

        }
        return user;
    }

}
